//
//  OrderFromViewController.m
//  JackDeve
//
//  Created by 王奎卿 on 2017/5/9.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "OrderFromViewController.h"
#import "SGSegmentedControl.h"
#import "AllOrderFromViewController.h"
#import "EvaluateViewController.h"

@interface OrderFromViewController ()<SGSegmentedControlDelegate,UIScrollViewDelegate>
{
    AllOrderFromViewController *_oneVC1;
    EvaluateViewController *_oneVC;
}
@property (nonatomic, strong) SGSegmentedControl *SG;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@end

@implementation OrderFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title =@"订单";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
    self.navigationController.navigationBar.barTintColor=[UIColor whiteColor];
    //创建一个添加按钮
    UIButton *navLeftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [navLeftBtn setBackgroundImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [navLeftBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navLeftBtn];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 1.添加所有子控制器
    [self setupChildViewController];
    
    [self setupSegmentedControl];
    
    [self showRightItem];
}

- (void)showRightItem
{
    self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [self.rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
   
   
    
  
    
}

- (void)clickRightItem:(UIButton *)sender {
    [_oneVC1.tableView setEditing:!_oneVC1.tableView.editing animated:YES];
    [_oneVC.tableView setEditing:!_oneVC.tableView.editing animated:YES];
    [sender setTitle:@"编辑" forState:UIControlStateNormal];
    
    if (!_oneVC1.tableView.editing) {
        [sender setTitle:@"编辑" forState:UIControlStateNormal];
    } else {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
    }
}

-(void)click
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setupSegmentedControl {
    
        NSArray *title_arr = @[@"全部订单",@"待评价"];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    
    _mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * title_arr.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    // 去除滚动效果
    _mainScrollView.scrollEnabled = NO;
    
    [self.view addSubview:_mainScrollView];
    
    
    self.SG = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44) delegate:self segmentedControlType:(SGSegmentedControlTypeStatic) titleArr:title_arr];
    //选中
    _SG.titleColorStateSelected =[UIColor blueColor];
    _SG.titleColorStateNormal =[UIColor grayColor];
    //下划线
    _SG.indicatorColor =[UIColor blueColor];;
    //底部样式
    _SG.segmentedControlIndicatorType=SGSegmentedControlIndicatorTypeBottom;
    [self.view addSubview:_SG];

}
- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
    
}
// 添加所有子控制器
- (void)setupChildViewController {
     __weak typeof (self) weakSelf = self;
    // 全部订单
    _oneVC1 = [[AllOrderFromViewController alloc] init];
    _oneVC1.butBlock = ^(NSString * st) {
        [weakSelf.rightBtn setTitle:st forState:UIControlStateNormal];
    };
   
    [self addChildViewController:_oneVC1];
    // 待评价
    _oneVC = [[EvaluateViewController alloc] init];
    [self addChildViewController:_oneVC];
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    // 2.把对应的标题选中
    [self.SG titleBtnSelectedWithScrollView:scrollView];
    
}

- (BOOL)shouldAutorotate
{
    return NO;
}

@end
